package com.yuhanginfo.modules.web.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yuhanginfo.common.utils.FileUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.WebArea;
import com.yuhanginfo.modules.web.entity.WebDict;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.WebAreaService;
import com.yuhanginfo.modules.web.service.WebSystemService;
import com.yuhanginfo.modules.web.service.WebYongUserService;
import com.yuhanginfo.modules.web.util.WebDictUtils;

/**
 * 注册管理
 * @author yinhao
 * @version 2017-10-30
 */
@Controller
@RequestMapping(value = "${frontPath}/register")
public class WebRegisterController extends BaseController {

	@Autowired
	private WebYongUserService webYongUserService;

	@Autowired
	private WebSystemService webSystemService;
	
	@Autowired
	private WebAreaService webAreaService;
	 
	@RequestMapping(value = "")
	public String index() {
		return "modules/web/webRegister";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "/toAssociator")
	public String toAssociator(Model model,HttpSession session) {
		model.addAttribute("logoInfo","企业认证");//顶部显示文字
		WebYongUser user = webSystemService.getUser(WebUserUtils.getYongUser().getId());//拿到最新的用户数据
		session.setAttribute("webUser", user);
		//不是一般用户则返回主页
		if(!"1".equals(user.getUserType())){
			session.setAttribute("user", user);
			return "redirect:"+ frontPath +"/webIndex";
		}
		model.addAttribute("user", user);
		if("0".equals(user.getExamineState())){
			//企业类型
			List<WebDict> companyType = WebDictUtils.getDictList("company_type");
			model.addAttribute("companyType", companyType);
			//公司规模
			List<WebDict> companySize = WebDictUtils.getDictList("company_size");
			model.addAttribute("companySize", companySize);
			//省
			List<WebArea> province = webAreaService.selectProvinceList();
			model.addAttribute("province", province);
			return "modules/web/webRegisterAssociator";
		}else if("1".equals(user.getExamineState())||"3".equals(user.getExamineState())){
			return "modules/web/webRegisterAssociatorView";
		}else{
			return "redirect:"+ frontPath +"/webIndex";
		}
	}
	
	/*
	 * 审核失败才进行跳转
	 * */
	@RequiresPermissions("user")
	@RequestMapping(value = "/toRepeatAssociator")
	public String toRepeatAssociator(Model model,HttpSession session) {
		model.addAttribute("logoInfo","企业认证");//顶部显示文字
		WebYongUser user = webSystemService.getUser(WebUserUtils.getYongUser().getId());//拿到最新的用户数据
		session.setAttribute("webUser", user);
		//不是一般用户则返回主页
		if(!"1".equals(user.getUserType())){
			return "redirect:"+ frontPath +"/webIndex";
		}
		model.addAttribute("user", user);
		if("3".equals(user.getExamineState())){
			//企业类型
			List<WebDict> companyType = WebDictUtils.getDictList("company_type");
			model.addAttribute("companyType", companyType);
			//公司规模
			List<WebDict> companySize = WebDictUtils.getDictList("company_size");
			model.addAttribute("companySize", companySize);
			//省
			List<WebArea> province = webAreaService.selectProvinceList();
			model.addAttribute("province", province);
			return "modules/web/webRegisterAssociator";
		}else{
			return "redirect:"+ frontPath +"/webIndex";
		}
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = "/associator")
	public String associator(WebYongUser user,Model model,MultipartFile image1,MultipartFile image2,MultipartFile image3,MultipartFile image4,MultipartFile image5) {
		try{
			model.addAttribute("logoInfo","企业认证");//顶部显示文字
			user.setId(WebUserUtils.getYongUser().getId());
			if(image1.getSize() != 0){
				user.setTitleImage(FileUtils.saveSimpleNewImage(user.getId(), image1.getOriginalFilename(), image1.getInputStream()));
			}else{
				user.setTitleImage("");
			}
			if(image2.getSize() != 0){
				user.setBusinessLicence(FileUtils.saveSimpleNewImage(user.getId(), image2.getOriginalFilename(), image2.getInputStream()));
			}else{
				user.setBusinessLicence("");
			}
			if(image3.getSize() != 0){
				user.setOpenBankAccountPermitCertificate(FileUtils.saveSimpleNewImage(user.getId(), image3.getOriginalFilename(), image3.getInputStream()));
			}else{
				user.setOpenBankAccountPermitCertificate("");
			}
			if(image4.getSize() != 0){
				user.setOrganizationCodeCertificate(FileUtils.saveSimpleNewImage(user.getId(), image4.getOriginalFilename(), image4.getInputStream()));
			}else{
				user.setOrganizationCodeCertificate("");
			}
			if(image5.getSize() != 0){
				user.setTaxRegistrationCertificate(FileUtils.saveSimpleNewImage(user.getId(), image5.getOriginalFilename(), image5.getInputStream()));
			}else{
				user.setTaxRegistrationCertificate("");
			}
			user.setExamineState("1");
			webYongUserService.save(user);
		}catch(Exception ex){
			logger.error(ex.getMessage(),ex);
		}
		
		return "modules/web/webRegisterAssociatorSuccess";
	}
	
	@RequestMapping(value = "nomal")
	public String nomal(WebYongUser user) {
		if(webYongUserService.existUserByLoginName(user)){
			return "modules/web/webRegister";
		}
		if(webYongUserService.existUserByEmail(user)){
			return "modules/web/webRegister";
		}
		if(webYongUserService.existUserByMobile(user)){
			return "modules/web/webRegister";
		}
		webYongUserService.saveUser(user);
		return "modules/web/webRegisterSuccess";
	}
	
	@RequestMapping(value = "checkLoginName",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLoginName(WebYongUser user) {
		boolean result = webYongUserService.existUserByLoginName(user);
		return !result;
	}
	
	@RequestMapping(value = "checkEmail",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkEmail(WebYongUser user) {
		boolean result = webYongUserService.existUserByEmail(user);
		return !result;
	}
	
	//验证电话不存在
	@RequestMapping(value = "checkMobile",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkMobile(WebYongUser user) {
		return !webYongUserService.existUserByMobile(user);
	}
	
	//验证电话存在
	@RequestMapping(value = "checkMobileExist",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkMobileExist(WebYongUser user) {
		boolean result = webYongUserService.existUserByMobile(user);
		return result;
	}
	
	@RequestMapping(value = "checkLinkmanEmail",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLinkmanEmail(WebYongUser user) {
		boolean result = webYongUserService.existLinkmanEmail(user);
		return !result;
	}
	
	@RequestMapping(value = "checkLinkmanPhone",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLinkmanPhone(WebYongUser user) {
		boolean result = webYongUserService.existLinkmanPhone(user);
		return !result;
	}
	
	//法律声明跳转
	@RequestMapping(value = "/legalDeclaration")
	public String legalDeclaration() {
		return "modules/web/legalDeclaration";
	}
}